package com.imooc.socialecom.helper;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisOperation {

    @Autowired
    private RedisTemplate redisTemplate;

    @Async
    public void asyncRedisStringSet(String key, Object data) {

        // 防止缓存雪崩（错开key的过期时间）
        long randomExpireSeconds = RandomUtil.randomLong(1000);
        redisTemplate.opsForValue().set(key, data, randomExpireSeconds, TimeUnit.SECONDS);
    }


}
